<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use think\Controller;
class Ariclecate extends Common
{
    
 
    public function index()
    {
    	  
   
        $list =  $this->CycleData();
        $this->assign('list',$list);
		
    	return view();
    }
public function add(){
	  if(request()->isPost()) {
            //构建数组
            $data = input('post.');
            $data['addtime'] = time();
            db('ariclecate')->insert($data);
            $result['code'] = 1;
            $result['msg'] = '分类添加成功!';
            $result['url'] = url('index');
            return $result;
        }else{
    
	 
	 $list =  $this->CycleData();
	 
	 
      $this->assign('list',$list);
	return view();
		}
}


public function CycleData($parent_id=0){
        $where['pid'] = $parent_id;
        $res = db('ariclecate')->where($where)->select();;
        if(empty($res)) return false;
        foreach($res as $k=>$v){
            $result[$v['id']]['id'] = $v['id'];
            $result[$v['id']]['title'] = $v['title'];
            $result[$v['id']]['pid'] = $v['pid'];
            $rf = $this->CycleData($v['id']);
            if($rf){
                foreach($rf as $k=>$vv){
                    $result[$vv['id']]['id'] = $vv['id'];
                    $result[$vv['id']]['title'] = "   |— ".$vv['title'];
                    $result[$vv['id']]['pid'] = $vv['pid'];
                }
            }
        }
        return $result;
    }









public function edit(){
	
	
        if(request()->isPost()) {
            $data = input('post.');
            db('ariclecate')->where('id',$data['id'])->update($data);
            $result['code'] = 1;
            $result['msg'] = '分类修改成功!';
            $result['url'] = url('index');
            return $result;
        }else{
            $id=input('id');
            $info=db('ariclecate')->where(array('id'=>$id))->find();
			
            $this->assign('info',$info);
        	 $list =  $this->CycleData();
	 
	 
      $this->assign('list',$list);
            return view();
        }
    
	
}

public function del(){
	
	 db('ariclecate')->where('id',input('param.id'))->delete();
        $this->redirect('index');
	
}

   
}